import 'package:efood_multivendor/controller/product_controller.dart';
import 'package:efood_multivendor/data/model/response/order_details_model.dart';
import 'package:efood_multivendor/data/model/response/order_model.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/custom_app_bar.dart';
import 'package:efood_multivendor/view/screens/review/widget/deliver_man_review_widget.dart';
import 'package:efood_multivendor/view/screens/review/widget/product_review_widget.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class RateReviewScreen extends StatefulWidget {
  final List<Details> orderDetailsList;
  final DeliveryMan? deliveryMan;
  final bool? isFoodReviewed;
  final bool isFromHome;
  final String? orderId;
  RateReviewScreen(
      {required this.orderDetailsList,
      required this.deliveryMan,
      this.isFoodReviewed,
      this.isFromHome = false,
      this.orderId});

  @override
  _RateReviewScreenState createState() => _RateReviewScreenState();
}

class _RateReviewScreenState extends State<RateReviewScreen>
    with TickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    bool isDeliv = false;
    if (widget.deliveryMan != null) {
      isDeliv = widget.deliveryMan?.reviewed ?? false;
    }
    _tabController = TabController(
        length: widget.deliveryMan == null ||
                isDeliv == true ||
                widget.isFoodReviewed == true
            ? 1
            : 2,
        initialIndex: 0,
        vsync: this);
    Get.find<ProductController>().initRatingData(widget.orderDetailsList);
  }

  @override
  Widget build(BuildContext context) {
    bool showDeliveryMan = false;
    bool? deliveryManReviewed;
    bool showFood = false;
    bool showBoth = false;
    if (widget.deliveryMan != null) {
      deliveryManReviewed = widget.deliveryMan!.reviewed ?? false;
    }
    if (widget.isFoodReviewed == true &&
        widget.deliveryMan != null &&
        widget.deliveryMan?.reviewed != true) {
      showDeliveryMan = true;
      showBoth = false;
      showFood = false;
    } else if (widget.isFoodReviewed == false && deliveryManReviewed == false) {
      showFood = false;
      showDeliveryMan = false;
      showBoth = true;
    } else {
      showFood = true;
      showBoth = false;
      showDeliveryMan = false;
    }

    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: CustomAppBar(
        title: 'rate_review'.tr,
      ),
      body: Column(children: [
        Center(
          child: Container(
            width: Dimensions.WEB_MAX_WIDTH,
            color: Theme.of(context).cardColor,
            child: TabBar(
                controller: _tabController,
                labelColor: Theme.of(context).textTheme.bodyMedium?.color,
                indicatorColor: Theme.of(context).primaryColor,
                indicatorWeight: 3,
                unselectedLabelStyle: sfRegular.copyWith(
                    color: Theme.of(context).disabledColor,
                    fontSize: Dimensions.fontSizeSmall),
                labelStyle:
                    sfMedium.copyWith(fontSize: Dimensions.fontSizeSmall),
                tabs: showBoth == true
                    ? [
                        Tab(text: 'delivery_man'.tr),
                        Tab(
                            text: widget.orderDetailsList.length > 1
                                ? 'items'.tr
                                : 'item'.tr),
                      ]
                    : showDeliveryMan == true
                        ? [
                            Tab(text: 'delivery_man'.tr),
                          ]
                        : showFood == true
                            ? [
                                Tab(
                                    text: widget.orderDetailsList.length > 1
                                        ? 'items'.tr
                                        : 'item'.tr),
                              ]
                            : []),
          ),
        ),
        Expanded(
            child: TabBarView(
          controller: _tabController,
          children: showBoth == true
              ? [
                  DeliveryManReviewWidget(),
                  ProductReviewWidget(),
                ]
              : showDeliveryMan == true
                  ? [
                      DeliveryManReviewWidget(),
                    ]
                  : showFood == true
                      ? [
                          ProductReviewWidget(),
                        ]
                      : [],
        )),
      ]),
    );
  }
}
